import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class represents the bar circuits show all circuits and all devices.
 * 
 * @author Christian Pérez Zavalija
 * @version 25-Jun-2013
 */
public class CircuitsBar extends Actor
{
    private Circuit[] circuits;
    private Device[] devices;
    
    /**
     * Add to world objects circuit and objects device with a position on the bar.
     */
    public void addedToWorld(World world)
    {
        int y=getY()-205;
        for(int i=0;i<4;i++)
        {
            getWorld().addObject(circuits[i],getX(),y);
            getWorld().addObject(devices[i],getX(),y+210);
            y+=50;
        }
    }
    
    /**
     * Initializes all circuits and all devices with pins repectives and scale the objects.
     */
    public CircuitsBar()
    {
        circuits=new Circuit[4];
        devices=new Device[4];
        
        XOR xor=new XOR();
        OR or=new OR();
        AND and=new AND();
        NOT not=new NOT();
        DipSwitch dipswitch=new DipSwitch();
        Display display=new Display();
        LED led=new LED();
        PushButton pushbutton=new PushButton();
         
        
        circuits[0]=xor;
        circuits[1]=or;
        circuits[2]=and;
        circuits[3]=not;
        devices[0]=dipswitch;
        devices[1]=display;
        devices[2]=led;
        devices[3]=pushbutton;
        
        for(Circuit c: circuits)
            c.scale();
        for(Device d: devices)
            d.scale();
    }
    
    /**
     * This method allows you to drag the bar circuit with all the objects that are in it. Depending on the circuit
     * or device is pressed creates a new object of it, and places it in the position of the mouse.
     */
    public void act() 
    {
        int y=getY()-205;
        World world=getWorld();
        Canvas canvas=(Canvas)world;
        if(Greenfoot.mouseDragged(this))    //Condition so that the object can be dragged with the mouse while pressing the left button.
        {
            MouseInfo mouse=Greenfoot.getMouseInfo();
            setLocation(mouse.getX(),mouse.getY());
            for(Circuit c: circuits){
                c.setLocation(getX(),y);
                y+=50;
            }
            for(Device d: devices){
                d.setLocation(getX(),y+10);
                y+=50;
            }
        }
        //for(Circuit c: circuits)
        if(Greenfoot.mousePressed(circuits[0]))
        {
            MouseInfo mouse=Greenfoot.getMouseInfo();
            XOR xor=new XOR();
            canvas.addObject(xor,mouse.getX(),mouse.getY());
        }
        if(Greenfoot.mousePressed(circuits[1]))
        {
            MouseInfo mouse=Greenfoot.getMouseInfo();
            OR or=new OR();
            canvas.addObject(or,mouse.getX(),mouse.getY());
        }
        if(Greenfoot.mousePressed(circuits[2]))
        {
            MouseInfo mouse=Greenfoot.getMouseInfo();
            AND and=new AND();
            canvas.addObject(and,mouse.getX(),mouse.getY());
        }
        if(Greenfoot.mousePressed(circuits[3]))
        {
            MouseInfo mouse=Greenfoot.getMouseInfo();
            NOT not=new NOT();
            canvas.addObject(not,mouse.getX(),mouse.getY());
        }
        if(Greenfoot.mousePressed(devices[0]))
        {
            MouseInfo mouse=Greenfoot.getMouseInfo();
            DipSwitch dipswitch=new DipSwitch();
            canvas.addObject(dipswitch,mouse.getX(),mouse.getY());
        }
        if(Greenfoot.mousePressed(devices[1]))
        {
            MouseInfo mouse=Greenfoot.getMouseInfo();
            Display display=new Display();
            canvas.addObject(display,mouse.getX(),mouse.getY());
        }
        if(Greenfoot.mousePressed(devices[2]))
        {
            MouseInfo mouse=Greenfoot.getMouseInfo();
            LED led=new LED();
            canvas.addObject(led,mouse.getX(),mouse.getY());
        }
        if(Greenfoot.mousePressed(devices[3]))
        {
            MouseInfo mouse=Greenfoot.getMouseInfo();
            PushButton pushbutton=new PushButton();
            canvas.addObject(pushbutton,mouse.getX(),mouse.getY());
        }
    }    
}
